package com.example.flowabledemo.controller;

import cn.hutool.core.util.StrUtil;
import com.example.flowabledemo.common.HttpCode;
import com.example.flowabledemo.data.dto.ResponsePageResult;
import com.example.flowabledemo.data.dto.ResponseResult;
import com.example.flowabledemo.service.AdminService;
import com.example.flowabledemo.service.FlowableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("admin")
@Api(value = "审批岗位相关操作", tags = "AdminController")
public class AdminController {

  @Autowired
  private HttpServletRequest request;
  @Autowired
  private FlowableService flowableService;
  @Autowired
  private AdminService adminService;

  @GetMapping("approval")
  public String approval() {
    return "admin/approval";
  }

  @GetMapping("getApproval")
  @ResponseBody
  public ResponsePageResult getApproval(String status,Integer page, Integer limit) {
    if(StrUtil.isBlank(status)){
      return new ResponsePageResult(HttpCode.VALIDATION, "入参status不能为空");
    }
    if (page == null || limit == null) {
      return new ResponsePageResult(HttpCode.VALIDATION, "入参page、limit不能为空");
    }
    if (page < 0 || page > 999) {
      return new ResponsePageResult(HttpCode.VALIDATION, "入参page范围：1~999");
    }
    if (limit < 0 || limit > 100) {
      return new ResponsePageResult(HttpCode.VALIDATION, "入参limit范围：1~100");
    }
    return flowableService.getApproval(status,page, limit);
  }

  @GetMapping("getApprovalHistory")
  @ResponseBody
  public ResponsePageResult getApprovalHistory(String status,Integer page, Integer limit) {
    if(StrUtil.isBlank(status)){
      return new ResponsePageResult(HttpCode.VALIDATION, "入参status不能为空");
    }
    if (page == null || limit == null) {
      return new ResponsePageResult(HttpCode.VALIDATION, "入参page、limit不能为空");
    }
    if (page < 0 || page > 999) {
      return new ResponsePageResult(HttpCode.VALIDATION, "入参page范围：1~999");
    }
    if (limit < 0 || limit > 100) {
      return new ResponsePageResult(HttpCode.VALIDATION, "入参limit范围：1~100");
    }
    return flowableService.getApprovalHistory(status,page, limit);
  }

  @ApiOperation(value = "审批请假流程", notes = "对请假流程进行审批")
  @GetMapping("approvalLeave/{processId}")
  public String approvalLeave(@PathVariable("processId") String processId) {
    if (StrUtil.isBlank(processId)) {
      throw new RuntimeException("入参processId不能为空：approvalLeave/{processId}");
    }
    request.setAttribute("vo", flowableService.getApprovalLeaveVo(processId));
    request.setAttribute("processId", processId);
    return "admin/approval_leave";
  }

  @PostMapping("approvalLeave/{processId}")
  @ResponseBody
  public ResponseResult approvalLeavePost(@PathVariable("processId") String processId,Integer approval,String message){
    if (StrUtil.isBlank(processId)) {
      return new ResponseResult(HttpCode.OK,"入参processId不能为空：approvalLeave/{processId}");
    }
    if(approval==null){
      return new ResponseResult(HttpCode.OK,"入参approval不能为空");
    }
    return adminService.approvalLeave(processId,approval,message);
  }
}
